<?
include 'includes/specs_functions.php';
echo printGoogleAnalytics();
#---------------------------------------------------
# Connect to PADS
$DB = getDB();

#-------------------------------$Session information
$THEME = $_POST["THEME"];
#---------------------------------------------------


$amount = 7;

$output = " <script type=\"text/javascript\" src=\"../scripts/tooltip/tooltip.js\"></script>
			<script>				
				$(document).ready(function(){
					$('.hot-spec-row').click(function(e) {
							var clicked = $(e.target);
							var tr = $(this).closest('tr');
							var specId = tr.attr('id');
							editSpec(specId);
					});
				});
				
				$(function(){
					$('.hot_ratings').children().not(':radio').hide();
					$('.hot_ratings').stars({
						disabled: true
					});
				});

						
						
			</script>
				<table width='100%' border='0' cellspacing='0' cellpadding='2'>";
	
	
$table = "((
				".SPEC_TBL_SPECS." as spec 
				LEFT JOIN ".TBL_CHARACTERS." as user
				ON user.".TBL_CHARACTERS_CICUID." = spec.".SPEC_TBL_SPECS_USER_ID.") 
				LEFT JOIN (SELECT ".SPEC_TBL_RATINGS_SPEC_ID.", sum(".SPEC_TBL_RATINGS_VALUE.") as value, count(".SPEC_TBL_RATINGS_ID.") as count FROM ".SPEC_TBL_RATINGS." group by ".SPEC_TBL_RATINGS_SPEC_ID.") as rate
				ON spec.".SPEC_TBL_SPECS_ID." = rate.".SPEC_TBL_RATINGS_SPEC_ID.")";
$col = array(
				"spec.".SPEC_TBL_SPECS_ID." as specId"
			   ,"spec.".SPEC_TBL_SPECS_USER_ID." as userId"
			   ,"spec.".SPEC_TBL_SPECS_LAYOUT." as layout"
			   ,"spec.".SPEC_TBL_SPECS_JOB_ID." as jobId"
			   ,"spec.".SPEC_TBL_SPECS_LEVEL." as level"
			   ,"spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
			   ,"spec.".SPEC_TBL_SPECS_PUBLIC." as public"
			   ,"spec.".SPEC_TBL_SPECS_CDATE." as cdate"
			   ,"spec.".SPEC_TBL_SPECS_UDATE." as udate"
			   ,"rate.value"
			   ,"rate.count"
			   ,"user.".TBL_CHARACTERS_NAME." as userName"
			   ,"user.".TBL_CHARACTERS_AVATAR." as Avatar"
			   ,"user.".TBL_CHARACTERS_SERVER." as Server"
			);
$where = array(
				"spec.".SPEC_TBL_SPECS_PUBLIC." = 'Y'"
			);
$orderBy = "rate.count";
$direction = "DESC";
$limit = array(0, $amount);

$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction, $limit);


$i = 1;
foreach ($result[3] as $row) {
	
	$jobName = GetClass($row['jobId']);

	$count = $row['count'];
	$value = $row['value'];
	if($count != 0){
		$average = floor($value / $count);
	}else{
		$average = 0;
		$count = 0;
	}
	$server = $row['Server'];
	if(strlen($server)>8){
		$server = substr($server,0,7).".";
	}
	
	$output .= "
		<tr id='".$row['specId']."' class='hot-spec-row cur-point spec-row-hover'>
			<td align=\"left\" width=\"10%\" style='font-size:10px;'>
				".$row['level']."
			</td>
			<td  width='10%'>
				<span class='with-tooltip normal-text bold-text' style='font-size:10px;' title=\"".str_replace("\"", "'", $row['description'])."\"><img src='".GetClassIcon($jobName)."' style='width:18px;height:18px' /></span>
			</td>
			<td align='left' width='10%'>
				<span class='with-tooltip' title=\"<table><tr><td><span class='normal-text bold-text' style='font-size:10px'>".$row['userName']."</span></td></tr><tr><td><img src='".$row['Avatar']."'></img></td></tr></table>\"><img src='".$row['Avatar']."' style='width:18px;height:18px' /></span>
			</td>
			
			<td align='center' width='40%' style='padding-left: 5px;'>
					<form class='hot_ratings hot-spec-row cur-point width-tooltip' action='' method='post' title='Rating: ".$average.", Votes: ".$count."'>";

					foreach (get_options() as $id => $title){
						$output .= 	"<input type='radio' name='rate'  value='".$id."' id='rate".$id."' ";
						if($id==$average){
							$output .= 	" checked";
						}
						$output .= 	" />";
					}
			
					$output .= 	"</form>
			</td>
			<td width='20%' align='right' style='font-size:10px;'>
				".$server."
			</td>
		</tr>";
	
	$i++;
	
}

	
echo $output;
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);
?>